package object.obj2D.graphics;

import javax.microedition.khronos.opengles.GL10;

import object.obj2D.Object2D;
import system.Device;
import system.Graphics;

public class Square extends Object2D{
	//------------------------------------------------------------
	/// Variable declare 
	//------------------------------------------------------------
	private float	m_width = 1.0f;
	private float	m_height = 1.0f;
	private GL10 gl;
	
	//------------------------------------------------------------
	/// Constructor 
	//------------------------------------------------------------
	public Square(float width, float height){
		gl = Graphics.GetGraphics();
		if (Device.IsEnableScaleFactor()){
			width *= Device.GetScaleFactorW();
			height *= Device.GetScaleFactorH();
		}
		this.m_width = width/2;
		this.m_height = height/2;
		this.m_vertices = new float [] {
	            -m_width, +m_height, 0.0f, // 0
	            +m_width, +m_height, 0.0f, // 1
	            +m_width, -m_height, 0.0f, // 2
			    -m_width, -m_height, 0.0f, // 3
		};
		this.m_indices = new short [] {  
				0, 2, 1, //
	            0, 3, 2, //
		};
		this.m_colors = new float [] {
				0f, 0f, 0f, 0f,
	            1f, 1f, 1f, 0f,
	            0f, 0f, 1f, 0f,
	            0f, 1f, 0f, 0f,
	            1f, 0f, 0f, 0f,
	            0f, 1f, 1f, 0f,
	            0f, 1f, 0f, 0f,
	            1f, 0f, 1f, 0f
	    };
		this.Init();
	}
	
	public void SetWidth(float width)
	{
		this.m_width = width/2;
		this.m_vertices = new float [] {
	            -m_width, +m_height, 0.0f, // 0
	            +m_width, +m_height, 0.0f, // 1
	            +m_width, -m_height, 0.0f, // 2
			    -m_width, -m_height, 0.0f, // 3
		};
		m_vertexBuffer = CreateFloatBuffer(m_vertices);
	}
	
	public void SetHeight(float height)
	{
		this.m_height = height/2;
		this.m_vertices = new float [] {
	            -m_width, +m_height, 0.0f, // 0
	            +m_width, +m_height, 0.0f, // 1
	            +m_width, -m_height, 0.0f, // 2
			    -m_width, -m_height, 0.0f, // 3
		};
		m_vertexBuffer = CreateFloatBuffer(m_vertices);
	}
	
	public float GetWidth()
	{
		return m_width * 2;
	}
	
	public float GetHeight()
	{
		return m_height * 2;
	}
	
	public void SetSize(float width, float height)
	{
		this.m_width = width/2;
		this.m_height = height/2;
		this.m_vertices = new float [] {
	            -m_width, +m_height, 0.0f, // 0
	            +m_width, +m_height, 0.0f, // 1
	            +m_width, -m_height, 0.0f, // 2
			    -m_width, -m_height, 0.0f, // 3
		};
		
		m_vertexBuffer = CreateFloatBuffer(m_vertices);
	}
	
	public void Render(){
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, m_vertexBuffer);
		//gl.glColorPointer(4, GL10.GL_FLOAT, 0, m_colorBuffer);
		gl.glDrawElements(GL10.GL_TRIANGLES, m_indices.length, 
				GL10.GL_UNSIGNED_SHORT, m_indexBuffer);
	}
}
